package com.bw.demo.video

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.demo.R
import com.bw.demo.adapter.FangAdapter
import com.bw.demo.adapter.TypeAdapter
import com.bw.demo.adapter.VideoAdapter
import com.bw.demo.base.BaseFragment
import com.bw.demo.databinding.FragmentVideoBinding
import com.bw.demo.detail.DetailActivity
import com.bw.demo.entity.TypeEntity
import com.bw.demo.entity.VideoEntity
import com.bw.demo.home.HomeViewModel


class VideoFragment : BaseFragment<FragmentVideoBinding>() {
    private lateinit var viewModel: HomeViewModel
    private lateinit var fangAdapter: FangAdapter
    private lateinit var typeAdapter: TypeAdapter
    val listvideo= mutableListOf<VideoEntity>()
    val listtype= mutableListOf<TypeEntity>()
    override fun initData() {
        viewModel= ViewModelProvider(this)[HomeViewModel::class.java]
        viewModel.getVideo("94349546935",1,10)
        viewModel.getType(1,10)
        viewModel.success.observe(this){
            listvideo.clear()
            listvideo.addAll(it.data)
            fangAdapter.notifyDataSetChanged()
        }
        viewModel.success2.observe(this){
            listtype.clear()
            listtype.addAll(it.data)
            typeAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        mDataBinding.recVideo.layoutManager= GridLayoutManager(context,2)
        fangAdapter= FangAdapter(listvideo)
        mDataBinding.recVideo.adapter=fangAdapter

        mDataBinding.recTypeTwo.layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        typeAdapter= TypeAdapter(listtype)
        mDataBinding.recTypeTwo.adapter=typeAdapter

        typeAdapter.setOnItemClickListener { adapter, view, position ->
            val channelid = typeAdapter.data.get(position).channelid
            viewModel.getVideo(channelid,1,10)
        }
        fangAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(context,DetailActivity::class.java)
            val bundle = Bundle()
            bundle.putString("video",listvideo[position].videopath)
            bundle.putString("name",listvideo[position].name)
            bundle.putString("title",listvideo[position].title)
            intent.putExtras(bundle)
            startActivity(intent)
        }
    }

    override fun getLyoutId(): Int {
        return R.layout.fragment_video
    }

}